package com.hikvision.cms.common.util;

public class HexDump {
    private static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};


    public static String dumpHexString(byte[] array) {
        return dumpHexString(array, 0, array.length);
    }


    public static String dumpHexString(byte[] array, int offset, int length) {
        StringBuilder result = new StringBuilder();

        /*  27 */
        byte[] line = new byte[16];
        /*  28 */
        int lineIndex = 0;

        /*  30 */
        result.append("\n0x");
        /*  31 */
        result.append(toHexString(offset));

        /*  33 */
        for (int i = offset; i < offset + length; i++) {
            /*  34 */
            if (lineIndex == 16) {
                /*  35 */
                result.append(" ");

                /*  37 */
                for (int j = 0; j < 16; j++) {
                    /*  38 */
                    if (line[j] > 32 && line[j] < 126) {
                        /*  39 */
                        result.append(new String(line, j, 1));
                    } else {
                        /*  41 */
                        result.append(".");
                    }
                }

                /*  45 */
                result.append("\n0x");
                /*  46 */
                result.append(toHexString(i));
                /*  47 */
                lineIndex = 0;
            }

            /*  50 */
            byte b = array[i];
            /*  51 */
            result.append(" ");
            /*  52 */
            result.append(HEX_DIGITS[b >>> 4 & 0xF]);
            /*  53 */
            result.append(HEX_DIGITS[b & 0xF]);

            /*  55 */
            line[lineIndex++] = b;
        }

        /*  58 */
        if (lineIndex != 16) {
            /*  59 */
            int count = (16 - lineIndex) * 3;
            /*  60 */
            count++;
            int j;
            /*  61 */
            for (j = 0; j < count; j++) {
                /*  62 */
                result.append(" ");
            }

            /*  65 */
            for (j = 0; j < lineIndex; j++) {
                /*  66 */
                if (line[j] > 32 && line[j] < 126) {
                    /*  67 */
                    result.append(new String(line, j, 1));
                } else {
                    /*  69 */
                    result.append(".");
                }
            }
        }

        /*  74 */
        return result.toString();
    }


    public static String toHexString(byte b) {
        return toHexString(toByteArray(b));
    }


    public static String toHexString(byte[] array) {
        return toHexString(array, 0, array.length);
    }


    public static String toHexString(byte[] array, int offset, int length) {
        char[] buf = new char[length * 2];

        int bufIndex = 0;
        for (int i = offset; i < offset + length; i++) {
            byte b = array[i];
            buf[bufIndex++] = HEX_DIGITS[b >>> 4 & 0xF];
            buf[bufIndex++] = HEX_DIGITS[b & 0xF];
        }

        return new String(buf);
    }


    public static String toHexString(int i) {
        return toHexString(toByteArray(i));
    }


    public static byte[] toByteArray(byte b) {
        byte[] array = new byte[1];
        array[0] = b;
        return array;
    }


    public static byte[] toByteArray(int i) {
        byte[] array = new byte[4];

        array[3] = (byte) (i & 0xFF);
        array[2] = (byte) (i >> 8 & 0xFF);
        array[1] = (byte) (i >> 16 & 0xFF);
        array[0] = (byte) (i >> 24 & 0xFF);

        return array;
    }


    private static int toByte(char c) {
        if (c >= '0' && c <= '9') return c - 48;
        if (c >= 'A' && c <= 'F') return c - 65 + 10;
        if (c >= 'a' && c <= 'f') return c - 97 + 10;
        throw new RuntimeException("Invalid hex char '" + c + "'");
    }


    public static byte[] hexStringToByteArray(String hexString) {
        int length = hexString.length();
        byte[] buffer = new byte[length / 2];

        for (int i = 0; i < length; i += 2) {
            buffer[i / 2] = (byte) (toByte(hexString.charAt(i)) << 4 | toByte(hexString.charAt(i + 1)));
        }

        return buffer;
    }
}

